layui.use(['form','layer','table'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    //列表
    var tableIns = table.render({
        elem: '#basicInformationList',
        url : '/basicInformation/listData',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,20,30],
        limit : 10,
        toolbar: true,
        id : "basicInformationListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
			{field: 'centralBudgetEnable', title: '是否申请中央预算内投资', minWidth:100, align:"center",templet:function(d){
                if (d.centralBudgetEnable === 1) {
                    return '是';
                } else {
                    return '否';
                } 
			}
			},
			{field: 'projectName', title: '项目名称', minWidth:100, align:"center"},
			{field: 'projectType', title: '项目类型', minWidth:100, align:"center"},
			{field: 'approveProjectName', title: '审批监管平台项目名称', minWidth:100, align:"center"},
			{field: 'approveCode', title: '审批监管平台代码', minWidth:100, align:"center"},
			{field: 'buildProperties', title: '建设性质', minWidth:100, align:"center"},
			{field: 'nationality', title: '国别', minWidth:100, align:"center"}, 
			{field: 'buildLocation', title: '建设地点', minWidth:100, align:"center"}, 
			{field: 'buildLocationDetail', title: '建设地点详情', minWidth:100, align:"center"},
			{field: 'buildLocationAddress', title: '建设详细地址', minWidth:100, align:"center"}, 
			{field: 'nationalIndustry', title: '国标行业', minWidth:100, align:"center"}, 
			{field: 'nationalIndustryId', title: '国标行业ID', minWidth:100, align:"center"},
			{field: 'involvedIndustry', title: '所属行业', minWidth:100, align:"center"},
			{field: 'involvedIndustryId', title: '所属行业ID', minWidth:100, align:"center"}, 
			{field: 'totalInvestment', title: '总投资', minWidth:100, align:"center"},  
			{field: 'planedStartDate', title: '拟开工日期', minWidth:100, align:"center"}, 
			{field: 'planedEndDate', title: '拟竣工日期', minWidth:100, align:"center"},  
			{field: 'mainBuildScale', title: '主要建设规模', minWidth:100, align:"center"}, 
			{field: 'mainBuildConten', title: '(年度)主要建设内容', minWidth:100, align:"center"},  
			{field: 'status', title: '状态', minWidth:100, align:"center"} 
			]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("basicInformationListTable",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                searchKey: $(".searchKey").val()
            }
        })
    });

    //添加
    function addBasicInformation(edit){
        var title = "添加";
        if (edit){
            title = "编辑";
        }
        var index = layui.layer.open({
            title : title,
            type : 2,
            area : ["500px","500px"],
            shade: 0,
            maxmin: true,
            content : "info.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
	                    body.find("#basicInformationId").val(edit.basicInformationId);
	                    body.find("#centralBudgetEnable").val(edit.centralBudgetEnable);
	                    body.find("#localSpecialBondEnable").val(edit.localSpecialBondEnable);
	                    body.find("#projectName").val(edit.projectName);
	                    body.find("#projectType").val(edit.projectType);
	                    body.find("#approveProjectName").val(edit.approveProjectName);
	                    body.find("#approveCode").val(edit.approveCode);
	                    body.find("#buildProperties").val(edit.buildProperties);
	                    body.find("#nationality").val(edit.nationality);
	                    body.find("#buildLocation").val(edit.buildLocation);
	                    body.find("#buildLocationDetail").val(edit.buildLocationDetail);
	                    body.find("#buildLocationAddress").val(edit.buildLocationAddress);
	                    body.find("#nationalIndustry").val(edit.nationalIndustry);
	                    body.find("#nationalIndustryId").val(edit.nationalIndustryId);
	                    body.find("#involvedIndustry").val(edit.involvedIndustry);
	                    body.find("#involvedIndustryId").val(edit.involvedIndustryId);
	                    body.find("#totalInvestment").val(edit.totalInvestment);
	                    body.find("#planedStartDate").val(edit.planedStartDate);
	                    body.find("#planedEndDate").val(edit.planedEndDate);
	                    body.find("#mainBuildScale").val(edit.mainBuildScale);
	                    body.find("#mainBuildConten").val(edit.mainBuildConten);
	                    body.find("#remark").val(edit.remark);
	                    body.find("#status").val(edit.status);
	                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
    }
    $(".add_btn").click(function(){
        addBasicInformation();
    });

    $(".edit_btn").click(function(){
        var checkStatus = table.checkStatus('basicInformationListTable'),
            data = checkStatus.data;
        if(data.length > 0){
            addBasicInformation(data[0]);
        }else{
            layer.msg("请选择需要修改的记录");
        }
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('basicInformationListTable'),
            data = checkStatus.data,
            idArr = [];
        if(data.length > 0) {
            for (var i in data) {
                idArr.push(data[i].id);
            }
            layer.confirm('确定删除选中的记录？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("/basicInformation/delBatch",{
                    idArr : idArr.toString()
                },function(data){
                    layer.close(index);
                    tableIns.reload();
                    if (data.data){
                        layer.msg("删除成功！");
                    } else {
                        layer.msg(data.msg);
                    }
                })
            })
        }else{
            layer.msg("请选择需要删除的记录");
        }
    })

})